<?php
class User_Model extends CI_Model{
	
	
	 function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
	
	function getData($limit, $offset){
		$sql="SELECT * from user order by id desc limit $offset, $limit";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->result_array();
		}else{
			return array();
		}
	}
	
	function get_detail_data($id){
		$sql="SELECT * from user where id='".$id."'";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->row_array();
		}else{
			return array();
		}
	}
	
	function getAllData(){
		$sql="SELECT * from user order by id desc";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->result_array();
		}else{
			return array();
		}
	}
	function count_all_num_rows(){
		return $this->db->count_all('user');
	}
	
	function insert_data($username,$gambar,$password,$aktif){
		
		$data=array('username'=>$username,
					'password'=>md5($password),
					'status_aktif'=>$aktif,
					'foto'=>$gambar);
		$insert=$this->db->insert('user',$data);
		return $insert;
	}
	
	function update_data($id,$username,$gambar,$password,$aktif){
		if($password!=""){
			$data=array('username'=>$username,
						'password'=>md5($password),
						'status_aktif'=>$aktif,
						'foto'=>$gambar);
		}else{
			$data=array('username'=>$username,
					'status_aktif'=>$aktif,
					'foto'=>$gambar);
		}
		$this->db->where('id',$id);
		$update=$this->db->update('user',$data);
		return $update;
	}
	
	function ganti_pass($id,$password){
		$data=array('password'=>md5($password));
		$this->db->where('id',$id);
		$update=$this->db->update('user',$data);
		return $update;
	}
	
	function delete_data($id){
		$data=array('id'=>$id);
		$this->db->where($data);
		$delete=$this->db->delete('user'); 
		return $delete;
	}

	

}
?>